45J - Planting Trees - CodeForces Solution


constructive algorithms *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
//#include <algorithm>
//#include <iostream>
//#include <vector>
//#include <string>
//#include <cstdio>
//#include <cstring>
//#include <cmath>
using namespace std;

#define endl '\n'

#define ll long long

// ll是64位版本
#define bp __builtin_popcountll

typedef std::pair<int, int> pii;

int n, m;
bool r = false;
int nums[105][105];

void solve() {
    cin >> n >> m;
    if (n > m) {
        swap(n, m);   // n < m
        r = true;
    }

    int total = n * m;
    if (n == 1) {
        if (m == 1) {
            cout << 1 << endl;
            return;
        }
        if (m == 2 || m == 3) {
            cout << -1 << endl;
            return;
        }
        int p = 1;
        for (int i = 1; i <= (m + 1) / 2; i += 1) {
            nums[0][p] = i;
            p += 2;
        }
        if (m & 1) {
            p = 2;
        } else {
            p = 0;
        }
        for (int i = ((m  + 1) / 2 + 1); i <= m; i++) {
            nums[0][p] = i;
            p += 2;
        }
        if (!r) {
            p = m & 1;
            for (int i = p; i < p + m; i++) {
                cout << nums[0][i] << " ";
            }
            cout << endl;
        } else {
            p = m & 1;
            for (int i = p; i < p + m; i++) {
                cout << nums[0][i];
                cout << endl;
            }
        }
        return;
    } else if (n == 2) {
        if (m == 1 || m == 2) {
            cout << -1 << endl;
            return;
        }
        int row = 0;
        for (int i = 1; i <= total / 2; i++) {
            nums[row][i - 1] = i;
            row = row ^ 1;
        }
        row = 1;
        for (int i = 1; i <= total / 2; i++) {
            nums[row][i - 1] = i + total / 2;
            row = row ^ 1;
        }
    } else {
        int num = 1;
        for (int j = 0; j < m ; j++) {
            for (int i = 0; i < n; i++) {
                nums[i][(i + j) % m] = num++;
            }
        }

    }


    if (!r) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << nums[i][j] << " ";
            }
            cout << endl;
        }
    } else {
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                cout << nums[i][j] << " ";
            }
            cout << endl;
        }
    }
}

int main() {


    ios::sync_with_stdio(false);
    cin.tie(NULL);

    solve();
    return 0;
}

/*

clang++ -fsanitize=address -o2 -fno-omit-frame-pointer -o main main.cpp


1 4 7 10
11 2 5 8
9 12 3 6

 */



Comments

Submit
0 Comments
More Questions

1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number